package com.qf.shop.home.controller;

import com.qf.shop.common.core.utils.R;
import com.qf.shop.system.api.CatgoryService;
import com.qf.shop.system.api.PromotionService;
import com.qf.shop.system.dto.CatgoryDto;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 首页推荐模块
 */
@RestController
public class HomeController {

    @Resource
    private PromotionService promotionService;

    @Resource
    private CatgoryService catgoryService;
    //获取

    //轮播图

//    /*@GetMapping("/catgory/sortList")
////    public ArrayList treeList() {
////            return catgoryService.treeList();
////    }*/

    @GetMapping("/promotion/list")
    public R list() {
        return R.ok().put("data", promotionService.limitList());
    }
    //商品类型轮播
    @GetMapping("/catgory/limitList")
    public R limitList(){
        //获取商品总数
        List<CatgoryDto> list = catgoryService.limitList();
        //一个组对应一个String用map对应key-value  有多个组用list装
        List<HashMap<String, ArrayList<CatgoryDto>>> data = new ArrayList<>();
        //有几个组
        int sum = list.size()/8;
        //商品数量
        int a = 0;
        //循环
        for (int i = 0; i < sum ; i++) {
            //组类型
            HashMap<String, ArrayList<CatgoryDto>> map = new HashMap<>();
            //组里面有多个对象
            ArrayList<CatgoryDto> arrayList = new ArrayList<>();

            for (int j = 0; j < list.size(); j++) {
                arrayList.add(list.get(a));
                a++;
                if (a%8==0){
                    break;
                }
            }
            map.put("data",arrayList);
            data.add(map);

        }



        return R.ok().put("data",data);
    }
}
